package com.devops.admin.graphql;

import com.devops.admin.model.*;
import com.devops.admin.po.DOSChartParamPo;
import com.devops.admin.po.DOSChartPo;
import com.devops.admin.po.DOSChartUserVo;
import com.devops.admin.service.DOSClusterService;
import com.devops.admin.service.DOSHelmChartService;
import com.devops.admin.service.DOSHelmChartTemplateService;
import com.devops.admin.service.DOSHelmChartUserRelationService;
import com.devops.admin.vo.DOSChartParamVo;
import com.devops.admin.vo.DOSPageVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName DOSHelmChartDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 08:50
 * @Version 1.0
 **/
@DgsComponent
public class DOSHelmChartDataFetcher {

    @Resource
    private DOSHelmChartService dosHelmChartService;
    @Resource
    private DOSHelmChartUserRelationService dosHelmChartUserRelationService;
    @Resource
    private DOSClusterService dosClusterService;
    @Resource
    private DOSHelmChartTemplateService helmChartTemplateService;


    /**
     * @Author heyabo
     * @Description 添加chart
     * @Date 15:38 2022/3/23
     * @Param [chart]
     * @return com.devops.admin.model.TableResult<com.devops.admin.model.DOSHelmChart>
     **/
    @DgsMutation
    @DOSLog(description = "添加chart")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSChartParamPo saveHelm(@InputArgument DOSHelmChart chart){
        return dosHelmChartService.saveChart(chart);
    }

    /**
     * @Author heyabo
     * @Description 更新chart
     * @Date 17:55 2022/3/23
     * @Param [chart]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "更新chart")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Boolean updateChart(@InputArgument DOSHelmChart chart){
        return dosHelmChartService.updateChart(chart);
    }

    /**
     * @Author heyabo
     * @Description 删除chart
     * @Date 08:48 2022/3/24
     * @Param []
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "删除chart")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Boolean deleteChart(@InputArgument Integer chartId){
        return dosHelmChartService.deleteChart(chartId);
    }

    /**
     * @Author heyabo
     * @Description 为chart分配人员
     * @Date 09:54 2022/3/24
     * @Param [relation]
     * @return boolean
     **/
    @DgsMutation
    @DOSLog(description = "为chart分配人员")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public boolean distributionChartUser(@InputArgument DOSHelmChartUserRelation relation){
        return dosHelmChartUserRelationService.distributionUser(relation);
    }

    /**
     * @Author heyabo
     * @Description 查询chart列表
     * @Date 16:24 2022/3/23
     * @Param [paramVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.po.DOSChartParamPo>
     **/
    @DgsQuery
    @DOSLog(description = "查询chart列表")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public TableResult<DOSChartParamPo> getChartList(@InputArgument DOSChartParamVo paramVo, @InputArgument DOSPageVo pageVo){
        return dosHelmChartService.getCharts(paramVo,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 查询chart绑定的人员列表
     * @Date 10:21 2022/3/24
     * @Param [chartId]
     * @return java.util.List<com.devops.admin.model.DOSHelmChartUserRelation>
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSChartUserVo> getChartUserList(@InputArgument Integer chartId){
        return dosHelmChartUserRelationService.getUserList(chartId);
    }

    /**
     * @Author heyabo
     * @Description 获取部署的chart列表
     * @Date 08:50 2022/3/31
     * @Param []
     * @return java.util.List<com.devops.admin.po.DOSChartPo>
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSChartPo> getCharts(@InputArgument Integer projectId){
        return dosHelmChartService.getDeployCharts(projectId);
    }

    /**
     * @Author heyabo
     * @Description 查询仓库列表
     * @Date 10:12 2022/3/31
     * @Param []
     * @return java.util.List<com.devops.admin.po.DOSEnvRepositoryPo>
     **/
    @DgsQuery
    @DOSLog(description = "查询仓库列表")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSHelmChartRepository> getEnvRepositories(){
        return dosHelmChartService.getEnvRepositories();
    }

    /**
     * @Author heyabo
     * @Description 获取helm的api地址
     * @Date 14:57 2022/4/8
     * @Param []
     * @return java.lang.String
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public String getChartUrl(){
        String helmWrapperDomain = dosClusterService.getHelmChartUrl(0);
        String api = DevOpsConstant.HelmWrapperAPIUrl.HELM_UPLOAD_CHART_REPO_POST;
        String url = (helmWrapperDomain+api).replaceAll(" ","");
        return url.trim();
    }

    /**
     * @Author heyabo
     * @Description 获取部署的chart列表
     * @Date 08:50 2022/3/31
     * @Param []
     * @return java.util.List<com.devops.admin.po.DOSChartPo>
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSHelmChartTemplate> getChartTemplates(@InputArgument Integer chartId,
                                                        @InputArgument Integer clusterId){
        return helmChartTemplateService.getChartTemplates(chartId, clusterId);
    }
}
